/**
 * Created by jiaoshou on 2016/3/20.
 */
//获取innerText
/**
 *
 * @param element
 * @returns {*}
 */
function getInnerText(element) {
    //能力检测
    return element.textContent ? element.textContent: element.innerText;
}
//设置innerText
/**
 *
 * @param element
 * @param content
 */
function setInnerText(element,content) {
    if (typeof element.textContent === "string") {
        element.textContent = content;
    }else{
        element.innerText = content;
    }
}

//兼容浏览器
//获取下一个紧邻的兄弟元素
function getNextElement(element) {
    //能力检测
    if (element.nextElementSibling) {
        return element.nextElementSibling;
    }else{
        var node = element.nextSibling;
        while(node && node.nodeType !== 1) {
            node = node.nextSibling;
            break;
        }
        return node;
    }
}

/**
 * 返回上一个元素
 * @param element
 * @returns {*}
 */
function  getPreviousElement(element) {
    if (element.previousElementSibling) {
        return element.previousElementSibling;
    }else{
        var el = element.previousSibling;
        while (el && el.nodeType !== 1) {
            el = el.previousSibling;
        }
        return el;
    }
}

/**
 * 返回第一个元素 firstElementChild的浏览器兼容
 * @param parent
 * @returns {*}
 */
function  getFirstElement(parent) {
    if (parent.firstElementChild) {
        return parent.firstElementChild;
    }else{
        var el = parent.firstChild;
        while (el && el.nodeType !== 1) {
            el = el.nextSibling;
        }
        return el;
    }
}

/**
 * 返回最后一个元素
 * @param parent
 * @returns {*}
 */
function getLastElement(parent) {
    if (parent.lastElementChild) {
        return parent.lastElementChild;
    }else{
        var el = parent.lastChild;
        while (el && el.nodeType !== 1) {
            el = el.previousSibling;
        }
        return el;
    }
}


/**
 * 获取当前元素的所有兄弟元素
 * @param element
 * @returns {Array}
 */
function siblings(element){
    if (!element) return;

    var elements = [];
    var el = element.previousSibling;
    while (el) {
        if (el.nodeType === 1) {
            elements.push(el);
        }
        el = el.previousSibling;
    }

    el = element.nextSibling;
    while (el) {
        if(el.nodeType === 1) {
            elements.push(el);
        }
        el = el.nextSibling;
    }
    return elements;
}


/**
 * 获取ID为id的元素
 * @param id
 * @returns {Element}
 */
function my$(id) {
    return document.getElementById(id);
}

/**
 * 获取当前时间
 * @param Data
 * @returns {string}
 */
function getDataString (Data) {
    var year =  Data.getFullYear();
    var month =  Data.getMonth() + 1;
    var data =  Data.getDate();
    var day = Data.getDay();
    var hour =  Data.getHours();
    var minute =  Data.getMinutes();
    var second =  Data.getSeconds();
    month = month < 10 ? "0" + month : month;
    data = data < 10 ? "0" + data : data;
    hour = hour < 10 ? "0" + hour : hour;
    minute = minute < 10 ? "0" + minute : minute;
    second = second < 10 ? "0" + second : second;
    return year + "-" + month + "-" + data + " " + hour + ":" + minute + ":" + second + " ";
}

/**
 * 添加和删除事件的兼容程序对象
 * @type {{addEvent: Function, removeEvent: Function}}
 */
var eventTools = {
    addEvent : function(element,eventName,fn) {
        if(typeof addEventListener === "function") {
            element.addEventListener(eventName,fn,false);
        }else{
            element.attachEvent("on"+eventName,fn);
        }
    },
    removeEvent : function(element,eventName,fn) {
        if (typeof removeEventListener === "function") {
            element.removeEventListener(eventName, fn, false);
        } else {
            element.detachEvent("on" + eventName, fn);
        }
    },
    getEvent : function (e) {
        return e ? e : window.e;
    },
    getPageX : function (e) {
        var scrollleft = document.documentElement.scrollLeft || document.body.scrollLeft;
        if(e.pageX) {
            return e.pageX;
        }else{
            return scrollleft + e.clientX;
        }
    },
    getPageY : function (e) {
        var scrolltop = document.documentElement.scrollTop || document.body.scrollTop;
        if(e.pageY) {
            return e.pageY;
        }else{
            return scrolltop + e.clientX;
        }
    }
}